home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
cgazv5n4.arc
/
STFILE.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1991-09-23
|
1KB
|
45 lines
//--- STFILE.CPP ------------------------- Listing 6 -----------
// Using new to create an array of string pointer objects
// by Bruce Eckel. See Listing 1 for copyright information.
//--------------------------------------------------------------
#include "strngptr.h"
main(int argc, char * argv[]) {
if(argc < 2) {
fprintf(stderr, "usage: stfile filename\n");
exit(1);
}
int lines = 0;
{ // count the lines in the file
FILE* fp = fopen(argv[1], "r");
if(fp == NULL) {
fprintf(stderr, "could not open %s\n", argv[1]);
exit(1);
}
const bufsize = 80;
char buf[bufsize];
while(fgets(buf, bufsize, fp))
lines++;
fclose(fp);
}
FILE* fp = fopen(argv[1], "r"); // open the file again
// An array with size determined at run-time:
string_ptr* sp = new string_ptr[lines];
// (constructor insures proper initialization of string_ptrs)
// sp now looks like a normal array (except
// that it must be deleted).
const bsz = 80; char buf[bsz];
for(int i = 0; i < lines; i++) {
if(!fgets(buf, bsz, fp)) break; // quit on end-of-file
buf[strlen(buf) -1] = '\0'; // erase terminating newline
sp[i]->append(buf); // append to the empty line
}
// Now we have the entire file in an array of string_ptr
// Perform editing here ...
// Then you can print out the array:
for(i = 0; i < lines; i++)
sp[i]->print();
delete sp;
}